package com.salim.leetcode.$61;

public class RotateList {
    public ListNode rotateRight(ListNode head, int k) {
        if(head==null){
            return null;
        }
        ListNode result = head;
        ListNode current = head;
        int length = 1;
        while (current.next!=null){
            length+=1;
            current = current.next;
        }
        current.next = head;
        k = k % length;
        int start = length-k;
        int end = length - k + length -1;
        int index = 0;
        while (head.next!=null){
            if(index==start){
                result = head;
            }
            if(index==end){
                head.next = null;
                break;
            }
            head = head.next;
            index++;
        }
        return result;
    }

    public static void main(String[] args) {
        RotateList r = new RotateList();
        ListNode four = new ListNode(4);
        four.next = new ListNode(5);
        ListNode three = new ListNode(3);
        three.next = four;
        ListNode two = new ListNode(2);
        two.next = three;
        ListNode one = new ListNode(1);
        one.next = two;
        r.rotateRight(one,2);
//        p.getPermutation(4,6);
    }
}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
